package com.practice;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.text.TextUtils;

import com.aiyige.MyApp;

public class EchoClientService extends IntentService {
    public static final String EXTRA_KEY_IP = "stb.EXTRA_KEY_IP";
    public static final String EXTRA_KEY_PORT = "stb.EXTRA_KEY_PORT";

    public EchoClientService() {
        super("EchoClientService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            String ip = intent.getStringExtra(EXTRA_KEY_IP);
            if (TextUtils.isEmpty(ip)) throw new Exception();
            int port = intent.getIntExtra(EXTRA_KEY_PORT, -1);
            if (port < 0) throw new Exception();
            new EchoClient().startClient(ip, port);
        } catch (Exception e) {

        }
    }

    public static void startService(String ip) {
        Intent intent = new Intent(MyApp.getAppContext(), EchoClientService.class);
        intent.putExtra(EXTRA_KEY_IP, ip);
        intent.putExtra(EXTRA_KEY_PORT, EchoServerService.SERVER_PORT);
        MyApp.getAppContext().startService(intent);
    }
}
